package com.lekang.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.domain.LeaveRequest;
import com.lekang.service.ILeaveRequestService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 请假Controller
 * 
 * @author ruoyi
 * @date 2025-10-14
 */
@RestController
@RequestMapping("/leave/leavemanage")
public class LeaveRequestController extends BaseController
{
    @Autowired
    private ILeaveRequestService leaveRequestService;

    /**
     * 查询请假列表
     */
    @PreAuthorize("@ss.hasPermi('leave:leavemanage:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveRequest leaveRequest)
    {
        startPage();
        List<LeaveRequest> list = leaveRequestService.selectLeaveRequestList(leaveRequest);
        return getDataTable(list);
    }

    /**
     * 导出请假列表
     */
    @PreAuthorize("@ss.hasPermi('leave:leavemanage:export')")
    @Log(title = "请假", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveRequest leaveRequest)
    {
        List<LeaveRequest> list = leaveRequestService.selectLeaveRequestList(leaveRequest);
        ExcelUtil<LeaveRequest> util = new ExcelUtil<LeaveRequest>(LeaveRequest.class);
        util.exportExcel(response, list, "请假数据");
    }

    /**
     * 获取请假详细信息
     */
    @PreAuthorize("@ss.hasPermi('leave:leavemanage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(leaveRequestService.selectLeaveRequestById(id));
    }

    /**
     * 新增请假
     */
    @PreAuthorize("@ss.hasPermi('leave:leavemanage:add')")
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveRequest leaveRequest)
    {
        int rows=leaveRequestService.insertLeaveRequest(leaveRequest);
        if(rows>0){
            return AjaxResult.success("新增请假成功").put("leaveRequestId", leaveRequest.getId());
        }
        return toAjax(rows);
    }

    /**
     * 修改请假
     */
    @PreAuthorize("@ss.hasPermi('leave:leavemanage:edit')")
    @Log(title = "请假", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveRequest leaveRequest)
    {
        return toAjax(leaveRequestService.updateLeaveRequest(leaveRequest));
    }

    /**
     * 删除请假
     */
    @PreAuthorize("@ss.hasPermi('leave:leavemanage:remove')")
    @Log(title = "请假", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveRequestService.deleteLeaveRequestByIds(ids));
    }
//    分页模糊查询
    @GetMapping("pagemo")
    public AjaxResult pageModel(LeaveRequest leaveRequest,
                                @RequestParam(required = false,defaultValue = "1") int pageNum,
                                @RequestParam(required = false,defaultValue = "2") int pageSize)
    {
        PageInfo<LeaveRequest> pageInfo = leaveRequestService.selectLeaveRequestPage(leaveRequest, pageNum, pageSize);
        return success(pageInfo);
    }


    /**
     * 查询请假列表
     */
    @PreAuthorize("@ss.hasPermi('leave:leavemanage:list')")
    @GetMapping("/elderly/{elderlyId}")
    public AjaxResult selectByElderlyId(@PathVariable Long elderlyId)
    {
//        return success(leaveRequestService.selectByElderlyId(elderlyId));
        List<LeaveRequest> result = leaveRequestService.selectByElderlyId(elderlyId);
        // 打印完整结果
        System.out.println("查询到的请假记录: " + result); // 打印完整结果
        return success(result);
    }

    @PreAuthorize("@ss.hasPermi('leave:leavemanage:list')")
    @GetMapping("/list1")
    public TableDataInfo list1(LeaveRequest leaveRequest)
    {
        startPage();
        List<LeaveRequest> list = leaveRequestService.selectLeaveRequestList1(leaveRequest);
        return getDataTable(list);
    }
}
